package com.frogs.ucenter.service.daoimpl.aop;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.frogs.component.permission.api.service.PermissionHelper;
import com.frogs.component.permission.entity.User;
import com.frogs.system.util.ChangeTypeEnum;
import com.frogs.ucenter.dao.SubFlowInfoDao;
import com.frogs.ucenter.dao.SubFlowInfoLogDao;
import com.frogs.ucenter.entity.SubFlowInfo;
import com.frogs.ucenter.entity.SubFlowInfoLog;

@Aspect
@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class) 
@Service("subFlowInfoLogAopImpl")
public class SubFlowInfoLogAopImpl {
	@Resource
	private SubFlowInfoLogDao subFlowInfoLogDao;
	@Resource
	private SubFlowInfoDao subFlowInfoDao;
	@Resource(name = "permissionHelper")
	private PermissionHelper permissionHelper;
	
	private static final Logger log = LoggerFactory.getLogger(TaskInfoLogAopImpl.class);

	
	@AfterReturning(pointcut="execution(* com.frogs.ucenter.controller.SubFlowInfoController.saveSubFlowInfo(..)) || "
			+ "execution(* com.frogs.ucenter.controller.SubFlowInfoController.updateSubFlowInfo(..)) || "
			+ "execution(* com.frogs.ucenter.controller.SubFlowInfoController.deleteSubFlowInfo(..))")
	public void doAfterReturning(JoinPoint jp)
	{
		try {
			HttpServletRequest req = (HttpServletRequest)jp.getArgs()[0];//切入点方法入参
			boolean flag = (Boolean) req.getAttribute("flag");
			String method = jp.getStaticPart().getSignature().getName();
			if(!flag){
				return;
			}
			SubFlowInfo subFlowInfo = new SubFlowInfo();
			int changeType = 0;
			User user = permissionHelper.getUser();
			int userId = user.getUserId();
			if(method.equals("saveSubFlowInfo")){
				subFlowInfo = (SubFlowInfo)jp.getArgs()[3];
				changeType = ChangeTypeEnum.ADD.getId();
				insertLog(subFlowInfo, changeType, userId);
			}else if(method.equals("updateSubFlowInfo")){
				subFlowInfo = (SubFlowInfo)jp.getArgs()[3];
				changeType = ChangeTypeEnum.UPDATE.getId();
				insertLog(subFlowInfo, changeType, userId);
			}else if(method.equals("deleteSubFlowInfo")){
				String deleteId = jp.getArgs()[3].toString();
				String[] arr = deleteId.split(",");
				changeType = ChangeTypeEnum.DELETE.getId();
				for(int i=0;i<arr.length;i++){
					subFlowInfo = new SubFlowInfo();
					subFlowInfo.setId(Integer.parseInt(arr[i]));
					insertLog(subFlowInfo, changeType, userId);
				}
			}
		} catch (Exception e) {
			log.error("func[doAfterReturning] jp[" + jp + "] error[" + e.getMessage() + "]", e);
		}
		
	}
	
	@Async
	private void insertLog(SubFlowInfo subFlowInfo, int changeType, int creater){
		SubFlowInfoLog subFlowInfoLog = new SubFlowInfoLog();
		subFlowInfoLog.setApplicablePlatformId(subFlowInfo.getApplicablePlatformId());
		subFlowInfoLog.setChangeType(changeType);
		subFlowInfoLog.setCreater(creater);
		subFlowInfoLog.setEndTime(subFlowInfo.getEndTime());
		subFlowInfoLog.setExpandCode(subFlowInfo.getExpandCode());
		subFlowInfoLog.setGiveCardId(subFlowInfo.getGiveCardId());
		subFlowInfoLog.setGiveCardName(subFlowInfo.getGiveCardName());
		subFlowInfoLog.setGiveMoney(subFlowInfo.getGiveMoney());
		subFlowInfoLog.setGivePoint(subFlowInfo.getGivePoint());
		subFlowInfoLog.setLevelId(subFlowInfo.getLevelId());
		subFlowInfoLog.setOldId(subFlowInfo.getId());
		subFlowInfoLog.setRemark(subFlowInfo.getRemark());
		subFlowInfoLog.setStartTime(subFlowInfo.getStartTime());
		subFlowInfoLog.setSubFlowName(subFlowInfo.getSubFlowName());
		subFlowInfoLog.setUrl(subFlowInfo.getUrl());
		subFlowInfoLogDao.insert(subFlowInfoLog);
	}
}
